package com.leyou.trade.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.auth.utils.UserContext;
import com.leyou.common.dto.PageDTO;
import com.leyou.trade.dto.OrderDTO;
import com.leyou.trade.dto.OrderFormDTO;
import com.leyou.trade.dto.OrderWithDetailDTO;
import com.leyou.trade.entity.OrderDetail;
import com.leyou.trade.dto.OrderListDTO;
import com.leyou.trade.entity.ItemComment;
import com.leyou.trade.service.ItemCommentService;
import com.leyou.trade.service.OrderService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("order")
@Validated
public class OrderController {

    private final OrderService orderService;

    private final ItemCommentService itemCommentService;

    public OrderController(OrderService orderService,ItemCommentService itemCommentService) {
        this.orderService = orderService;
        this.itemCommentService = itemCommentService;
    }

    /**
     * 创建订单
     * @param orderFormDTO  订单信息
     * @return 订单id
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderFormDTO orderFormDTO){
        Long orderId = orderService.createOrder(orderFormDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(orderId);
    }

    /**
     * 根据id查询订单
     * @param orderId  订单id
     * @return 订单对象
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderDTO> queryOrderById(@PathVariable("id") Long orderId){
        return ResponseEntity.ok(new OrderDTO(orderService.getById(orderId)));
    }

    /**
     * 查询订单状态
     * @param orderId 订单id
     * @return 订单状态
     */
    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> queryOrderStatusById(@PathVariable("id") Long orderId){
        return ResponseEntity.ok(orderService.queryOrderStatusById(orderId));
    }

    /**
     * 添加用户评论
     * @param comment
     * @return
     */
    @PostMapping("/comment")
    public ResponseEntity<Boolean> firstComment(@RequestBody @Valid ItemComment comment){
        return ResponseEntity.status(HttpStatus.CREATED).body(itemCommentService.comment(comment));
    }

    /**
     * 获取用户评论
     * @param orderId
     * @return
     */
    @GetMapping("/comment/{orderId}")
    public ResponseEntity<ItemComment> getComment(@PathVariable("orderId") Long orderId){
        return ResponseEntity.status(HttpStatus.CREATED).body(itemCommentService.getComment(orderId));
    }

    /**
     * 添加用户追评
     * @param orderId
     * @param afterComment
     * @return
     */
    @PutMapping("/addComment/{orderId}")
    public ResponseEntity<Boolean> addComment(@RequestParam("afterComment") @NotBlank String afterComment,@PathVariable("orderId") Long orderId ){
        return ResponseEntity.status(HttpStatus.CREATED).body(itemCommentService.addComment(orderId,afterComment));
    }


    /**
     * 查询订单展示信息
     * @param status 订单状态码1-6
     * @return
     */
    @GetMapping(value = "list/{status}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<PageDTO<OrderListDTO>> getOrderByUserIdAndStatus(@Min (0L) @Max (6L) @PathVariable("status") Integer status
            ,@RequestParam(value="page",defaultValue ="1") @Min(1) Integer page){
        Long userId = UserContext.getUser().getId();
        return ResponseEntity.ok(orderService.getOrderByUserIdAndStatus(userId,status,page));
    }

    @PostMapping("cancel/{orderId}")
    public ResponseEntity<Boolean> cancelOrder (@PathVariable("orderId")Long orderId) {

        return ResponseEntity.ok(orderService.cancelOrder(orderId));
    }

    //订单查询
    @GetMapping("search")
    public ResponseEntity<PageDTO<OrderWithDetailDTO> > queryOrderByKey( @RequestParam(value = "key",required = false)String key,
                                                                         @RequestParam(value = "status",required = false)Integer status,
                                                                         @RequestParam(value = "page",defaultValue = "1")Integer page,
                                                                         @RequestParam(value = "rows",defaultValue = "5")Integer rows){
        return ResponseEntity.ok(orderService.queryOrderByKey(key,status,page,rows));
    }
    //发货提醒
    @GetMapping("sendMsg")
    public ResponseEntity<Void>  sendMsg(@RequestParam("orderId") Long orderId) {
        orderService.sendMsg(orderId);
        return ResponseEntity.ok().build();

    }
}
